// const WxUtil = require('../../../utils/WxUtil.js')
// const apiUrl_fanyi = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttrans";
// const apiUrl_hecheng = "https://api.ai.qq.com/fcgi-bin/aai/aai_tts";
// const app = getApp();
const innerAudioContext = wx.createInnerAudioContext();

const plugin = requirePlugin("WechatSI")
// 获取**全局唯一**的语音识别管理器**recordRecoManager**
const manager = plugin.getRecordRecognitionManager()

Page({
  data: {
    text: "",
    type: "h2e",
    translateText: "",
    translateVoicePath:""
  },

  /**
   * 翻译
   */
  translateText: function () {
    if(this.data.text=='')
    {
      return;
    }

    let that = this;
    let lfrom = 'zh_CN'
    let lto = 'en_US'

    if(this.data.type=="e2h")
    {
      lfrom = 'en_US'
      lto = 'zh_CN'
    }

    wx.showLoading();

    plugin.translate({
      lfrom: lfrom,
      lto: lto,
      content: this.data.text,
      tts: true,
      success: (resTrans) => {

        let passRetcode = [
          0, // 翻译合成成功
          -10006, // 翻译成功，合成失败
          -10007, // 翻译成功，传入了不支持的语音合成语言
          -10008, // 翻译成功，语音合成达到频率限制
        ]

        if (passRetcode.indexOf(resTrans.retcode) >= 0) {
          that.setData({
            translateText: resTrans.result,
            translateVoicePath: resTrans.filename || "",
          });
          console.info(that.data.translateText);
          console.info(that.data.translateVoicePath);
          this.playVoice();
        }
      },
      fail: function (resTrans) {
        console.error("调用失败", resTrans)
      },
      complete: resTrans => {
        wx.hideLoading()
      }
    })

  },

  /**
   * 修改翻译类型
   */
  changeFyType: function (event) {
    let dataset = event.target.dataset;
    let type = dataset.type;
    
    this.setData({
      type: type
    });
  },
  /**
   * 
   */
  bindInputValue(event) {
    let value = event.detail.value;
    this.setData({
      text: event.detail.value,
    });
  },

  playVoice: function () {
    wx.playBackgroundAudio({
      dataUrl: this.data.translateVoicePath,
      success: (res) => {
        //this.playAnimationStart()
      },
      fail: (res) => {
        //this.playAnimationEnd()
      },
      complete: function (res) {
        //console.log("complete played");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  onShareAppMessage: function() {

  }
})